using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.IO;
using System.Data;
using System.ComponentModel;
using System.Drawing.Design;

namespace LocalizeMe.Sources
{

    [Editor(typeof(LocalizeMe.Sources.SourceEditor), typeof(UITypeEditor))]
    public class XmlSource:ResourceSource
    {
        private string _pathToXmlFiles;
        private string _fileExtension="lang";

        public string FileExtension
        {
            get
            {
                return _fileExtension;
            }
            set
            {
                _fileExtension = value;
            }
        }

        public string PathToXmlFiles
        {
            get
            {
                return _pathToXmlFiles;
            }
            set
            {
                _pathToXmlFiles = value;
            }
        }

        public XmlSource()
        { }

        public XmlSource(string name,string pathToXmlFiles,string fileExtension)
        {
            _pathToXmlFiles=pathToXmlFiles;
            this._name = name;
            this._fileExtension = fileExtension;
        }

        public override void Load()
        {
            if (string.IsNullOrEmpty(_pathToXmlFiles))
                return ;

            if (!Directory.Exists(_pathToXmlFiles))
                return ;

            DirectoryInfo languageDir = new DirectoryInfo(_pathToXmlFiles);

            try
            {
                FileInfo[] files = languageDir.GetFiles("*." + _fileExtension);
                foreach (FileInfo file in files)
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(file.FullName);

                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Columns.Count > 1)
                    {
                        if (ds.Tables[0].Columns.Contains("value") &&
                            ds.Tables[0].Columns.Contains("key"))
                        {
                            string[] nameParts = file.Name.Split('.');
                            string lang = string.Empty;

                            //Modify with Path class methods
                            if (nameParts.Length > 1)
                                lang = nameParts[nameParts.Length - 2];

                            if (!string.IsNullOrEmpty(lang))
                            {
                                ResourceItemCollection col = new ResourceItemCollection();

                                foreach (DataRow dr in ds.Tables[0].Rows)
                                    col.Add(new ResourceItem(dr["key"].ToString(), dr["value"]));

                                this.Items.Add(lang, col);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

            return ;
        }

        public override void Save()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override System.Collections.ObjectModel.Collection<string> GetAvailableCultures()
        {
            if (string.IsNullOrEmpty(_pathToXmlFiles))
                return null;

            if (!Directory.Exists(_pathToXmlFiles))
                return null;

            Collection<string> languages = new Collection<string>(); 

            DirectoryInfo languageDir = new DirectoryInfo(_pathToXmlFiles);


            FileInfo[] files = languageDir.GetFiles("*." + _fileExtension);
            foreach (FileInfo file in files)
            {
                string[] langTmp = file.Name.Split('.');
                if (langTmp.Length > 0 && !string.IsNullOrEmpty(langTmp[0]))
                {
                    if (!languages.Contains(langTmp[0]))
                        languages.Add(langTmp[0]);
                }
            }

            return languages;
        }

        
    }
}
